package com.zjcloud.jwgl.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.CacheEvict;
import com.zjcloud.jwgl.mapper.AcScheduleConstraintMapper;
import com.zjcloud.jwgl.domain.AcScheduleConstraint;
import com.zjcloud.jwgl.service.IAcScheduleConstraintService;
import com.zjcloud.common.utils.DateUtils;

/**
 * 排课约束条件Service业务层处理
 * 
 * @author wxl
 * @date 2024-09-13
 */
@Service
public class AcScheduleConstraintServiceImpl implements IAcScheduleConstraintService 
{
    @Autowired
    private AcScheduleConstraintMapper acScheduleConstraintMapper;
    
    // @Autowired
    // 由于使用了@Cacheable和@CacheEvict注解,RedisTemplate已不再直接使用
    // private RedisTemplate<String, Object> redisTemplate;

    /**
     * 查询排课约束条件
     * 
     * @param id 排课约束条件主键
     * @return 排课约束条件
     */
    @Override
    @Cacheable(value = "schedule_constraint", key = "'constraint:' + #id")
    public AcScheduleConstraint selectAcScheduleConstraintById(Long id)
    {
        return acScheduleConstraintMapper.selectAcScheduleConstraintById(id);
    }

    /**
     * 查询排课约束条件列表
     * 
     * @param acScheduleConstraint 排课约束条件
     * @return 排课约束条件
     */
    @Override
    public List<AcScheduleConstraint> selectAcScheduleConstraintList(AcScheduleConstraint acScheduleConstraint)
    {
        return acScheduleConstraintMapper.selectAcScheduleConstraintList(acScheduleConstraint);
    }

    /**
     * 新增排课约束条件
     * 
     * @param acScheduleConstraint 排课约束条件
     * @return 结果
     */
    @Override
    @CacheEvict(value = "schedule_constraint", allEntries = true)
    public int insertAcScheduleConstraint(AcScheduleConstraint acScheduleConstraint)
    {
        acScheduleConstraint.setCreateTime(DateUtils.getNowDate());
        return acScheduleConstraintMapper.insertAcScheduleConstraint(acScheduleConstraint);
    }

    /**
     * 修改排课约束条件
     * 
     * @param acScheduleConstraint 排课约束条件
     * @return 结果
     */
    @Override
    @CacheEvict(value = "schedule_constraint", key = "'constraint:' + #acScheduleConstraint.id")
    public int updateAcScheduleConstraint(AcScheduleConstraint acScheduleConstraint)
    {
        acScheduleConstraint.setUpdateTime(DateUtils.getNowDate());
        return acScheduleConstraintMapper.updateAcScheduleConstraint(acScheduleConstraint);
    }

    /**
     * 批量删除排课约束条件
     * 
     * @param ids 需要删除的排课约束条件主键
     * @return 结果
     */
    @Override
    @CacheEvict(value = "schedule_constraint", allEntries = true)
    public int deleteAcScheduleConstraintByIds(Long[] ids)
    {
        return acScheduleConstraintMapper.deleteAcScheduleConstraintByIds(ids);
    }

    /**
     * 删除排课约束条件信息
     * 
     * @param id 排课约束条件主键
     * @return 结果
     */
    @Override
    @CacheEvict(value = "schedule_constraint", key = "'constraint:' + #id")
    public int deleteAcScheduleConstraintById(Long id)
    {
        return acScheduleConstraintMapper.deleteAcScheduleConstraintById(id);
    }
    
    /**
     * 获取指定学期的所有启用约束条件
     * 
     * @param semester 学期
     * @return 约束条件列表
     */
    @Override
    @Cacheable(value = "schedule_constraint", key = "'semester:' + #semester")
    public List<AcScheduleConstraint> getEnabledConstraints(String semester)
    {
        AcScheduleConstraint query = new AcScheduleConstraint();
        query.setSemester(semester);
        query.setEnabled("1"); // 1表示启用
        return acScheduleConstraintMapper.selectAcScheduleConstraintList(query);
    }
    
    /**
     * 批量更新约束条件权重
     * 
     * @param constraints 约束条件列表
     * @return 结果
     */
    @Override
    @CacheEvict(value = "schedule_constraint", allEntries = true)
    public int updateConstraintWeights(List<AcScheduleConstraint> constraints)
    {
        int rows = 0;
        for (AcScheduleConstraint constraint : constraints)
        {
            constraint.setUpdateTime(DateUtils.getNowDate());
            rows += acScheduleConstraintMapper.updateAcScheduleConstraint(constraint);
        }
        return rows;
    }
}